<?php

namespace App\Listeners\Collect;

use App\Consts\GlobalCounterConst;
use App\Events\Collect\CancelCollectEvent;
use App\Listeners\BaseListener;

class CancelCollectListener extends BaseListener
{
    /**
     * Handle the event.
     *
     * @param CancelCollectEvent $event
     * @return bool
     */
    public function handle(CancelCollectEvent $event)
    {
        $this->saveFileLog(__CLASS__, $event);

        $collect = $event->collect;

        // 删除[index_search_collect]表记录
        service()->CollectService->delSearchCollect($collect['collect_id'], $collect['user_id']);

        // 更新收藏夹内容总数<total> -1
        service()->PackageService->changeTotal([
            'user_id' => $collect['user_id'],
            'package_id' => $collect['package_id'],
            'trend' => GlobalCounterConst::DECREASE
        ]);

        // 队列记录日志
        service()->LogCountService->createLogByQueue($event->logCountLoadData);

        return true;
    }
}
